// Battleships.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include "Board.h"
#include "Opponent.h"
#include "point.h"

using namespace std;

	Board playerBoard;
	Board playerBoardGuesses;
	Board oponentBoard;
	Board openentBoardGuesses;
	Opponent oponent;
	int populateMode = 1; // 0 = player populates own board , 1 = auto populate player board for testing
	bool debug = true; // 1 = verbose , 0 = normal


	// extract cords from input string
	int* getChords(string s)
	{
		int store[4];
		int j = 0;
		int sx = 0;
		int sy = 0;
		int ex = 0;
		int ey = 0;
		for ( int i = 0; i < s.length() ; i++ )
		{
			if (isdigit(s[i]))
			{
				if ( j > 4 )
				{
					break;
				}
				store[j] = s[i]-48; 
				j++;
			}
		}
		return store;
	}
	
	// dispose of objects / finish game
	void cleanUp()
	{
		
	}

	// main game loop
	void executeGame()
	{
		cout << "playing game" << endl;
		string cordString = "";
		Point hitCords;
		int* cords;
		Point oponentGuess;
		int x;
		int y;
		int ox;
		int oy;
		// while either player has remaining ships
		while ( oponentBoard.shipsLeft() || playerBoard.shipsLeft() )
		{
			
			// redraw players boards to reflect changes
			cout << "Your board.........................................." << endl;
			playerBoard.printBoard();
			cout << "Your guess board...................................." << endl;
			playerBoardGuesses.printBoard();

			// prompt player for guess 
			cout << "Please make a guess" << endl;
			getline(cin,cordString);
			cords = getChords(cordString);

			// check opponent's board for hit or miss
			x = cords[0];
			y = cords[1];
			if ( oponentBoard.hitMiss(x,y) )
			{
				cout << "HIT!" << endl;
				playerBoardGuesses.setBoardValue(x,y,'x');
			}
			else
			{
				cout << "MISS!" << endl;
				playerBoardGuesses.setBoardValue(x,y,'o');
			}

			// opponent's turn same as players
			cout << "oponent making guess" << endl;
			oponentGuess = oponent.makeGuess(&openentBoardGuesses);
			ox = oponentGuess.x;
			oy = oponentGuess.y;
			cout << "Opponet guesses x = " << ox << " y = " << oy << endl;
			if ( playerBoard.hitMiss(ox,oy) )
			{
				cout << "HIT!" << endl;
				openentBoardGuesses.setBoardValue(ox,oy,'x');
				openentBoardGuesses.setHit(ox,oy);
			}
			else
			{
				cout << "MISS!" << endl;
				openentBoardGuesses.setBoardValue(ox,oy,'o');
			}

			if ( debug )
			{
				cout << "Oponent board............................" << endl;
				oponentBoard.printBoard();
				cout << "Oponent guess board......................" << endl;
				openentBoardGuesses.printBoard();
			}
			// repeat loop
		}	

		cout << "GAME OVER!" << endl;
	}

	// set up the board for play
	void initialise()
	{
		if ( populateMode == 0 )
		{
			int placeRetVal = 0;
			string cordString = "";
			int* chords;
			cout << "Welcome to battleships" << endl;
			cout << "Please indicate the positions of your 4 ships using space as a delimiter e.g. \"1 1 2 2\" for a ship with start cord (1,1) and end cord (2,2)...." << endl;
			cout << "Ships should be 3 points long" << endl;
			for ( int i = 0; i < 4; i++)
			{
				playerBoard.printBoard();
				cout << "Please indicate the position of ship " << i << endl;
				getline(cin,cordString);
				chords = getChords(cordString);
				placeRetVal = playerBoard.placeShip(chords[0],chords[1],chords[2],chords[3]);
				if ( placeRetVal != 0 )
				{
					cout << "invalid ship cords entered please try again!" << endl;
					if ( i > 0 )
					{
						i--;
					}
				}
			}
		}

		if ( populateMode == 1 )
		{
			playerBoard.populate(); // switch off for to allow player to make own board!
		}

		oponentBoard.populate();

		if ( debug )
		{
			oponentBoard.printBoard();
		}
	}

int _tmain(int argc, _TCHAR* argv[])
{
	initialise();
	executeGame();
	
	if (debug)
	{
		//pause to display output before exiting
		while (true)
		{
		}
	}
	return 0;
}

